package com.wlm.service.impl;
//
//import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
//import com.baomidou.mybatisplus.core.conditions.Wrapper;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//
//
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.wlm.mapper.PgUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wlm.dao.PgUserDAO;
import com.wlm.dto.rep.UserQueryReqDto;
import com.wlm.dto.resp.PageRespDto;
import com.wlm.entity.PgUser;
import com.wlm.service.PgUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//import org.springframework.util.ObjectUtils;

import java.util.List;


/**
 * <p>
 * 服务实现类 mybatis plus
 * </p>
 *
 * @author PIGS
 * @since 2020-05-19
 */
//@Service
//public class PgUserServiceImpl extends ServiceImpl<PgUserMapper, PgUser> implements PgUserService {
//
//    @Autowired
//    private PgUserMapper pgUserMapper;
//
//    /**
//     * 查询用户并分页
//     *
//     * @return
//     */
//    @Override
//    public List<PgUser> queryUserList() {
//        AbstractWrapper wrapper = new QueryWrapper();
////        Wrapper<PgUser> wrapper = new QueryWrapper<>();
////        wrapper.orderByDesc("ucreatetime");
//        List<PgUser> pgUsersPage = pgUserMapper.selectList(wrapper);
//        return pgUsersPage;
//    }
//}

/**
 * mybatis
 */
@Service

public class PgUserServiceImpl implements PgUserService {

    @Autowired
    private PgUserDAO pgUserDAO;

    /**
     * 查询用户并分页
     *    1.使用PageHelper
     *
     * PageHelper.startPage(pageNum , pageSize);PageHelper.orderBy("A B");
     * 其中A为排序依据的字段名，B为排序规律，desc为降序，asc为升序
     * 或者一步到位
     * String orderBy="字段名 排序规律"；
     * PageHelper.startPage(pageNum, pageSize, orderBy)；
     * 2.使用Mybatis排序
     * XXXExample example = new XXXExample();
     * example.setOrderByClause("字段名1 ASC/DESC,字段名2 ASC/DESC,...");
     * @return
     * @param
     */
    @Override
    public PageRespDto<PgUser> queryUserList(UserQueryReqDto req) {
        String orderBy = "ucreatetime desc";

        PageHelper.startPage(req.getPageNum(), req.getPageSize(), orderBy);
//        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<PgUser> userList = pgUserDAO.selectByParam(req);

        PageInfo<PgUser> pageInfo = new PageInfo<>(userList);
//        log.info("总行数：{}", pageInfo.getTotal());
//        log.info("总页数：{}", pageInfo.getPages());
        System.out.println("总行数：{}"+ pageInfo.getTotal());
        System.out.println("总页数：{}"+ pageInfo.getPages());
        // 列表复制
//        List<UserQueryRespDto> list = CopyUtil.copyList(userList, UserQueryRespDto.class);

        PageRespDto<PgUser> pageRespDto = new PageRespDto<>();
        pageRespDto.setTotal(pageInfo.getTotal());
        pageRespDto.setList(userList);
        return pageRespDto;
    }

    @Override
    public boolean removeById(String id) {
        return pgUserDAO.delete(id);
    }

    @Override
    public boolean save(PgUser pgUser) {
        return pgUserDAO.save(pgUser);
    }

    @Override
    public boolean updateById(PgUser pgUser) {
        return pgUserDAO.update(pgUser);
    }
}